package com.example.spring_java_oj.service.problem;

import com.example.spring_java_oj.Util.FileUtil;
import com.example.spring_java_oj.common.Constants;
import com.example.spring_java_oj.mapper.ProblemMapper;
import com.example.spring_java_oj.model.Problem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;

/*
    从mysql中获取题目
 */
@Service
@Slf4j
public class ProblemService {
    @Autowired
    private ProblemMapper problemMapper;
    public List<Problem> getList(){
        List<Problem> list = problemMapper.getProblems();
        log.info(list.toString());
        return list;
    }

    public Problem getProblem(Integer id, HttpServletRequest request){
        Problem problem = problemMapper.getProblem(id);
//        HttpSession session = request.getSession();
//        String path ="./tmp/" + session.getAttribute("username") + id + "/";
//        File workDir = new File(path);
//        if(!workDir.exists()){
//            workDir.mkdirs();
//        }
//        String temPath = path + "finalCodeFile.txt";
//        File file = new File(temPath);
//        if(!file.exists()){
//            return problem;
//        }
//        String tem = FileUtil.readFile(temPath);
//        problem.setTemplateCode(tem);
        return problem;
    }

    public Problem getProblem_Name(String title, String description){
        Problem problem = problemMapper.getProblemByName(title, description);
        return problem;
    }

    public Problem getProblemStart(Integer id){
        return problemMapper.getProblem(id);
    }

    public Integer updateProblem(Problem problem){
        return problemMapper.updateProblem(problem);
    }

    public Integer deleteProblem(Integer id){
        return problemMapper.delete(id);
    }

    public Integer insert(Problem problem){
        return problemMapper.insert(problem);
    }


    public void updateProblemAnswer(Problem problem) {
        problemMapper.updateProblemAnswer(problem);
    }

}
